/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistence;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import model.Paciente;

/**
 *
 * @author Cristian
 */
public class PacienteDAO {
    private static PacienteDAO instance = new PacienteDAO();
    private PacienteDAO(){
    }
    public static PacienteDAO getInstace(){
     return instance;
    }
    
    
    public void save(Paciente paciente) throws SQLException, ClassNotFoundException{
        Connection conn = null;
        Statement st = null;

        try {
              conn = DataBaseLocator.getInsLocator().getConnection();
              st = conn.createStatement();
              st.execute("insert into paciente (nome, dtNascimento, profissao)" +
                        " values ('" + paciente.getNome() + "', '" + paciente.getDtNascimento() 
                      + "', '" + paciente.getProfissao() + "')");
            } catch(SQLException e) {
                throw e;
            } finally {
                closeResources(conn, st);
            }
        
    }
    
    public void closeResources(Connection conn, Statement st){
      try {
            if(st!=null) st.close();
            if(conn!=null) conn.close();
          } catch(SQLException e) {
                    
          }
    }

    public Paciente buscaPacientePorNome(String nome) {
       Connection conn = null;
       Statement st = null;
       Paciente p = new Paciente();  
        try{          
                conn = DataBaseLocator.getInsLocator().getConnection();
                st = conn.createStatement();
                st.executeQuery("Select * from Paciente f where f.nome like '%"+nome+"%'");
                ResultSet rs = st.getResultSet();            
                 p.setId(rs.getInt("id"));
                 p.setDtNascimento(rs.getDate("dtNascimento"));
                 p.setNome(rs.getString("nome"));
                 p.setProfissao(rs.getString("profissao"));
                 return p;
            } catch (Exception e) {
               e.printStackTrace();
               return null;
            }finally{
                closeResources(conn, st);
            }
    }
}
